package com.weiqiao.evaluate.service.impl;

import com.google.gson.Gson;
import com.weiqiao.evaluate.domain.Evluate4Entity;
import com.weiqiao.evaluate.repository.Evluate4Repository;
import com.weiqiao.evaluate.service.Evluate4Service;
import com.weiqiao.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 胡家豪
* @date 2020-10-23
*/
@Service("Evluate4ServiceImpl")
public class Evluate4ServiceImpl implements Evluate4Service {

    @Autowired
    private Evluate4Repository evluate4Repository;


    @Override
    public int countNum(int company_id, Bson filter) {
        int count = evluate4Repository.countNum(company_id, filter);
        return count;
    }

    @Override
    public List<Evluate4Entity> findList(int company_id, Bson filter, Pageable pageable) {
        List<String> array = evluate4Repository.getList(company_id, filter, pageable);
        List<Evluate4Entity> list = new ArrayList<>();
        if (CollectionUtils.isEmpty(array)) {
            return list;
        }
        for (String info : array) {
            Evluate4Entity entity = new Gson().fromJson(info, Evluate4Entity.class);
            list.add(entity);
        }
        return list;
    }

    @Override
    public Evluate4Entity getEntity(int companyId, Bson filter) {
        String info = evluate4Repository.getOne(companyId, filter);
        if (StringUtils.isBlank(info)) return new Evluate4Entity();
        return new Gson().fromJson(info, Evluate4Entity.class);
    }


    @Override
    public boolean addInfo(int company_id, String json) {
    return evluate4Repository.addInfo(company_id, json);
    }

    @Override
    public boolean updateInfo(int company_id, Bson filter, Document doc) {
    return evluate4Repository.updateInfo(company_id, filter, doc);
    }




}